package com.sam.im.heiqun.uis.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wangguodong on 2018/1/5.
 */

public class AddGroupSetQuestActivity extends BaseSwipeBackActivity {

    @BindView(R.id.pre_v_back)
    ImageView preVBack;
    @BindView(R.id.ok)
    TextView ok;
    @BindView(R.id.linear_question)
    LinearLayout linear_question;
    @BindView(R.id.linear_result)
    LinearLayout linear_result;
    @BindView(R.id.linear_fee)
    LinearLayout linear_fee;
    @BindView(R.id.linear_yzinfo)
    LinearLayout linear_yzinfo;
    @BindView(R.id.edit_quest)
    EditText edit_quest;
    @BindView(R.id.edit_result)
    EditText edit_result;
    @BindView(R.id.edit_fee)
    EditText edit_fee;
    @BindView(R.id.edit_days)
    EditText edit_days;//天数
    @BindView(R.id.edit_yzinfo)
    EditText edit_yzinfo;
    @BindView(R.id.linear_days)
    LinearLayout linearDays;

    private int type = 0;//0=只显示问题；1=显示问题和答案；2=显示加群费用；3=验证信息；4=显示问题，做回答
    private String quest = "";
    private String result = "";
    private String fee = "";
    private String days = "";//天数
    private String yzinfo = "";

    public static void start(Activity activity, int type, int REQUEST_CODE, String quest, String result, String days) {

        Intent intent = new Intent(activity, AddGroupSetQuestActivity.class);
        intent.putExtra("type", type);
        intent.putExtra("quest", quest);
        intent.putExtra("result", result);
        intent.putExtra("days", days);
        activity.startActivityForResult(intent, REQUEST_CODE);

    }

    public static void start(Activity activity, int type, int REQUEST_CODE, String fee, String days) {

        Intent intent = new Intent(activity, AddGroupSetQuestActivity.class);
        intent.putExtra("type", type);
        intent.putExtra("fee", fee);
        intent.putExtra("days", days);
        activity.startActivityForResult(intent, REQUEST_CODE);

    }

    public static void start(Activity activity, int type, int REQUEST_CODE) {

        Intent intent = new Intent(activity, AddGroupSetQuestActivity.class);
        intent.putExtra("type", type);
        activity.startActivityForResult(intent, REQUEST_CODE);

    }

    @Override
    protected String getTitleText() {
        type = getIntent().getIntExtra("type", 0);
        if (2 == type) {
            return "设置费用";
        } else if (3 == type) {
            return "验证信息";
        } else if (4 == type) {
            return "回答问题";
        }
        return "设置问题";
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {

        ok.setText("完成");
        ok.setVisibility(View.VISIBLE);

        quest = getIntent().getStringExtra("quest");
        result = getIntent().getStringExtra("result");
        fee = getIntent().getStringExtra("fee");
        days = getIntent().getStringExtra("days");

        if (null != quest && !TextUtils.isEmpty(quest)) edit_quest.setText(quest);
        if (null != result && !TextUtils.isEmpty(result)) edit_result.setText(result);
        if (null != fee && !TextUtils.isEmpty(fee)) edit_fee.setText(fee);
        if (null != days && !TextUtils.isEmpty(days)) edit_days.setText(days);

        if (0 == type) {
            linear_question.setVisibility(View.VISIBLE);
        } else if (1 == type) {
            linear_question.setVisibility(View.VISIBLE);
            linear_result.setVisibility(View.VISIBLE);
        } else if (2 == type) {
            linear_fee.setVisibility(View.VISIBLE);
            linearDays.setVisibility(View.VISIBLE);
        } else if (3 == type) {
            linear_yzinfo.setVisibility(View.VISIBLE);
        } else if (4 == type) {
            linear_question.setVisibility(View.VISIBLE);
            linear_result.setVisibility(View.VISIBLE);
            edit_quest.setText(fee);
            edit_quest.setEnabled(false);

        }

    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_add_group_set_quest;
    }

    @OnClick({R.id.ok, R.id.pre_v_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.pre_v_back:
                scrollToFinishActivity();
                break;
            case R.id.ok:
                String quest = edit_quest.getText().toString();
                if (0 == type) {
                    if (TextUtils.isEmpty(quest)) {
                        showToast("请输入问题！");
                    } else {
                        Intent intent = new Intent();
                        intent.putExtra("quest", quest);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                } else if (1 == type) {
                    String result = edit_result.getText().toString();
                    if (TextUtils.isEmpty(quest)) {
                        showToast("请输入问题！");
                    } else if (TextUtils.isEmpty(result)) {
                        showToast("请输入问题的答案！");
                    } else {
                        Intent intent = new Intent();
                        intent.putExtra("quest", quest);
                        intent.putExtra("result", result);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                } else if (2 == type) {
                    String fee = edit_fee.getText().toString();
                    String days = edit_days.getText().toString();
                    double a = 0;
                    int day = 0;
                    try {
                        a = Double.parseDouble(fee);
                        day = Integer.parseInt(days);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (TextUtils.isEmpty(fee) || a <= 0) {
                        showToast("请输入加群需要支付的费用！");
                    }else if(TextUtils.isEmpty(days) || day <= 0){
                        showToast("请输入天数！");
                    } else {
                        Intent intent = new Intent();
                        intent.putExtra("fee", fee);
                        intent.putExtra("days", days);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                } else if (3 == type) {
                    yzinfo = edit_yzinfo.getText().toString();
                    if (null == yzinfo || TextUtils.isEmpty(yzinfo)) {
                        showToast("请输入验证信息！");
                    } else {
                        Intent intent = new Intent();
                        intent.putExtra("yzinfo", yzinfo);
                        setResult(RESULT_OK, intent);
                        finish();
                    }

                } else if (4 == type) {
                    String result = edit_result.getText().toString();
//                    if (null==quest || TextUtils.isEmpty(quest)){
//                        showToast("请输入问题！");
//                    } else
                    if (null == result || TextUtils.isEmpty(result)) {
                        showToast("请输入问题的答案！");
                    } else {
                        Intent intent = new Intent();
//                        intent.putExtra("quest", quest);
                        intent.putExtra("result", result);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                }
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
